package com.koron.order.maintain.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.koron.bean.base.Response;
import com.koron.bean.query.PageQuery;
import com.koron.common.core.config.ThreadLocalContext;
import com.koron.order.common.bean.po.OrderDispatch;
import com.koron.order.maintain.bean.dto.MaintainDetailDto;
import com.koron.order.maintain.bean.dto.MaintainTwinDto;
import com.koron.order.maintain.bean.entity.MaintainPo;
import com.koron.order.maintain.bean.query.MaintainQuery;
import com.koron.order.maintain.bean.vo.MaintainDetailVo;
import com.koron.order.maintain.bean.vo.MaintainVo;
import com.koron.order.maintain.service.MaintainService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.ADOConnection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * (Maintain)表控制层
 *
 * @author zhouj
 * @since 2021-09-13 17:42:15
 */
@Api(tags = "日常维护工单主表")
@RestController
@RequestMapping("/maintain/")
public class MaintainController {

    @Autowired
    private MaintainService maintainService;

    private String getCurrDs() {
        return ThreadLocalContext.get().getCurrDs();
    }

    @ApiOperation(value = "新增或保存维护工单信息")
    @PostMapping("insertOrUpdate")
    public Response<String> insertOrUpdate(@RequestBody MaintainDetailDto dto) {
        return Response.ok(ADOConnection.runTask(getCurrDs(), maintainService, "insertOrUpdate", String.class, dto));
    }

    @ApiOperation(value = "新增维护工单信息")
    @PostMapping("insert")
    public Response<String> insert(@RequestBody MaintainPo maintain) {
        return Response.ok(ADOConnection.runTask(getCurrDs(), maintainService, "insert", String.class, maintain));
    }

    @ApiOperation(value = "更新")
    @PutMapping("update")
    public Response<Integer> update(MaintainPo maintainPo) {
        return Response.ok(ADOConnection.runTask(getCurrDs(), maintainService, "update", Integer.class, maintainPo));
    }

    @ApiOperation(value = "根据id删除 ")
    @DeleteMapping("deleteById")
    public Response<Integer> deleteById(String id) {
        return Response.ok(ADOConnection.runTask(getCurrDs(), maintainService, "deleteById", Integer.class, id));
    }

    @ApiOperation(value = "根据id查询详情")
    @GetMapping("queryById")
    public Response<MaintainDetailVo> queryById(String id) {
        return Response.ok(ADOConnection.runTask(getCurrDs(), maintainService, "queryById", MaintainDetailVo.class, id));
    }

    @ApiOperation(value = "根据计划id查询详情")
    @GetMapping("queryByPlanId")
    public Response<PageInfo> queryByPlanId(String planId, PageQuery pageInfo) {
        PageHelper.startPage(pageInfo.getPage(), pageInfo.getPageSize(), pageInfo.getOrderBy());
        List<MaintainVo> result = ADOConnection.runTask(getCurrDs(), maintainService, "queryByPlanId", List.class, planId);
        return Response.ok(new PageInfo(result));
    }

    @ApiOperation(value = "查询分页列表")
    @GetMapping("queryList")
    public Response<PageInfo> queryList(MaintainQuery query, PageQuery pageQuery) {
        return Response.ok(ADOConnection.runTask(getCurrDs(), maintainService, "queryList", PageInfo.class, query, pageQuery));
    }

    @ApiOperation(value = "提交维护工单状态 工单id 提交后状态(工作票专用)")
    @GetMapping("submitTicketMaintain")
    public Response<Integer> submitTicketMaintain(@RequestParam("maintainId") String maintainId,
                                      @RequestParam("status") String status) {
        return Response.ok(ADOConnection.runTask(getCurrDs(), maintainService, "submitTicketMaintain", Integer.class, maintainId, status));
    }

    @ApiOperation(value = "提交维护工单状态 工单id 提交后状态 ")
    @PostMapping("submitMaintain")
    public Response<Integer> submitMaintain(@RequestParam("maintainId") String maintainId,
                                      @RequestParam("status") String status, @RequestBody(required = false) OrderDispatch orderDispatch) {
        return Response.ok(ADOConnection.runTask(getCurrDs(), maintainService, "submitMaintain", Integer.class, maintainId, status, orderDispatch));
    }

    @ApiOperation(value = "单个作废工单")
    @DeleteMapping("deprecated")
    public Response<String> deprecated(String orderId) {
        Assert.isTrue(StringUtils.isNotEmpty(orderId));
        return Response.ok(ADOConnection.runTask(getCurrDs(), maintainService, "deprecated", String.class, orderId));
    }

    @ApiOperation(value = "水光孪生设备维保饼图")
    @GetMapping("/queryTwinMaintain")
    public Response<MaintainTwinDto> queryTwin(@RequestParam("eqId") String eqId,
                                 @RequestParam("eqName") String eqName,
                                 @RequestParam(value = "year") Integer year) {
        cn.hutool.core.lang.Assert.isFalse(StrUtil.isBlank(eqId), "请补充设备");
        if (ObjectUtil.isNull(year)) {
            year = DateUtil.year(new Date());
        }
        MaintainTwinDto maintainTwinDto = ADOConnection.runTask(getCurrDs(), maintainService, "queryTwin", MaintainTwinDto.class, eqId);
        if (ObjectUtils.isEmpty(maintainTwinDto)){
            return Response.ok(new MaintainTwinDto(year,eqId,eqName));
        }
        maintainTwinDto.setYear(year);
        return Response.ok(maintainTwinDto);
    }

}
